import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.awt.print.PrinterException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@WebServlet(name = "MySession", value = "/MySession")
public class MySession extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String heading;
        HttpSession session = request.getSession(true);
        Integer accessCount = (Integer)session.getAttribute("accessCount");
        if(accessCount==null){
            accessCount = new Integer(0);
            heading = "欢迎第一次访问";
        }
        else{
            heading = "欢迎再次访问";
            accessCount = new Integer(accessCount.intValue()+1);
        }
        Date date = new Date(session.getCreationTime());
        session.setAttribute("accessCount",accessCount);
        out.print("您的会话信息"+
                "<br>"+"会话ID："+session.getId()+
                "<br>"+"首次访问时间"+new Date(session.getCreationTime())+
                "<br>"+"首次访问时间"+session.getLastAccessedTime()+
                "<br>"+"最后的访问次数"+accessCount);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
